package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.GenerateStaticHtml;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Transactional
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private GenerateStaticHtml generateStaticHtml;

    @Override
    public void add(Setmeal setmeal, Integer[] checkGroupIds) {

        //添加套餐基本信息，并返回它的id
        setmealDao.add(setmeal);

        //遍历checkGroupIds，添加套餐和检查组的关联关系
        if(checkGroupIds!=null && checkGroupIds.length>0){
            Stream.of(checkGroupIds).forEach(checkGroupId->{
                setmealDao.setSetmealIdAndCheckGroupId(setmeal.getId(),checkGroupId);
            });
        }


    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        int currentPage=  queryPageBean.getCurrentPage();
        int pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //分页
        PageHelper.startPage(currentPage,pageSize);

        //调用数据访问层
       Page<Setmeal> page= setmealDao.selectByCondtion(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }


    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmeal(Integer id) {
        return setmealDao.findCheckGroupIdsBySetmeal(id);
    }


    @Override
    public void edit(Setmeal setmeal, Integer[] checkGroupIds) {

        //修改套餐基本信息
        setmealDao.update(setmeal);

        //删除套餐与检查组的关系
        setmealDao.deletCheckGroupIdsBySetmealId(setmeal.getId());

        //重新建立套餐与检查组的关系
        if(checkGroupIds!=null&&checkGroupIds.length>0){
            Stream.of(checkGroupIds).forEach(cgId->{
                setmealDao.setSetmealIdAndCheckGroupId(setmeal.getId(),cgId);
            });
        }


    }


    @Override
    public void delete(Integer setmeaId) {

        //删除套餐与检查组之间的关系
        setmealDao.deletCheckGroupIdsBySetmealId(setmeaId);

        //删除套餐基本信息
        setmealDao.deleteById(setmeaId);



    }


    @Override
    public List<Setmeal> findAll() {
        return setmealDao.selectByCondtion(null);
    }


    @Override
    public Setmeal findDetailById(Integer id) {
        return setmealDao.findDetailById(id);
    }


    @Override
    public void generateStaticHtml() {
        generateStaticHtml.generateStaticHtml();

    }


    @Override
    public List<Map> getSetmealOrderCount() {
        return setmealDao.getSetmealOrderCount();
    }
}
